/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

import { pickedFaceData } from '@amcax/base';
import {
  NumberInputFormat,
  PickInputRJSFFormat,
} from '@app-cad/common/components/command-form/constants';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { useTools } from '@app-cad/common/hooks/useTools';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useState } from 'react';

type FormData = {
  base: pickedFaceData;
  offset: number;
};

type Params = {
  base: string;
  offset: number;
};

function CommandThickness({ title, onClose, props }: CurvedCmdDialogParams) {
  const initData = {
    base: null,
    offset: 0,
  };
  const [formData, setFormData] = useState<FormData>(initData);

  const { picker } = useTools();

  useAsyncEffect(async () => {
    if (props == null) {
      setFormData(initData);
      return;
    }

    const params: Params = props.params;
    const { base, offset } = params;

    await initEditDependency([base]);

    setFormData({
      base: picker.generatePickObjByPickID(base) as pickedFaceData,
      offset,
    });
  }, [props?.shape]);

  const isFormDataValid = () => {
    const { base, offset } = formData;
    return base != null && offset != null && offset != 0;
  };

  const checkAndFinishDraw = async () => {
    if (!isFormDataValid()) {
      return;
    }

    await execute(false);

    onClose();
  };

  const {
    initEditDependency,
    initingEdit,
    clear,
    confirmCommand,
    isLoading,
    isPreviewLoading,
  } = useCommand('cmd-smooth', {
    commandName: 'CreateThickness',
    editDxid: props?.shape,
    onClose,
    finishDraw: checkAndFinishDraw,
  });

  const preview = async () => {
    clear();
    if (!isFormDataValid()) {
      return;
    }

    await execute(true);
  };

  const execute = (preview: boolean) => {
    const { base, offset } = formData;

    return confirmCommand(
      {
        a: {
          base: base.meshId,
          offset,
        } as Params,
      },
      preview,
    );
  };

  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      base: {
        type: 'string',
        format: PickInputRJSFFormat.PickSurface,
      },
      offset: {
        type: 'number',
        title: '厚度',
        format: NumberInputFormat.Length,
      },
    },
  };

  return (
    <PickableCommandForm
      disabled={initingEdit}
      submitLoading={isLoading}
      previewLoading={isPreviewLoading}
      onClose={onClose}
      onPreview={preview}
      title={title}
      schema={schema}
      formData={formData}
      onChange={(v) => {
        setFormData(v);
      }}
      extraUiSchema={{
        base: {
          'ui:placeholder': '请输入曲面',
          'ui:autofocus': true,
        },
      }}
      submitText='完成'
      onSubmit={checkAndFinishDraw}
      noValidate
    />
  );
}

export const cmdThickness: CurvedBaseCommand = {
  id: 'CommandThickness',
  label: 'command.thickness',
  icon: 'commands/curved/thickeness',
  create: (label, onClose, props) => (
    <CommandThickness
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
